import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login/'
import Reg from '@/views/reg'
import Layout from '@/views/Layout'
import Home from '@/views/Home'
import ArtCategory from '@/views/ArtCategory'
import ArtList from '@/views/ArtList'
import UserInfo from '@/views/UserInfo'
import ChangeAvatar from '@/views/ChangeAvatar'
import ResetPwd from '@/views/ResetPwd'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/login',
      component: Login
    },
    {
      path: '/reg',
      component: Reg
    },
    {
      // 首页
      path: '/',
      component: Layout,
      children: [
        // 主页(首页的二级路由)
        {
          path: '/',
          component: Home
        },
        // 文章分类
        {
          path: '/artcategory',
          component: ArtCategory
        },
        // 文章列表
        {
          path: '/artlist',
          component: ArtList
        },
        // 基本资料
        {
          path: '/userinfo',
          component: UserInfo
        },
        // 更换头像
        {
          path: '/changeavatar',
          component: ChangeAvatar
        },
        // 重置密码
        {
          path: '/resetpwd',
          component: ResetPwd
        }
      ]
    }
  ]
})

// 白名单 =>  就是表示 这些个页面是不需要登录的
const WHITE_LIST = ['/login', '/reg']

router.beforeEach((to, from, next) => {
  console.log(to)
  if (WHITE_LIST.includes(to.path)) {
    // 这是属于不登录也能看的页面  直接放行
    next()
  } else {
    // 表示去的是 需要登录的 才能看的页面
    // 需要判断登录? token
    if (localStorage.getItem('cms-91')) {
      // 登录 => 直接放行
      next()
    } else {
      // 没有登录 => 去登录页
      next('/login')
    }
  }
})

export default router
